part of '../repository.dart';

class _PurchasedSubscriptionDto extends Equatable {
  const _PurchasedSubscriptionDto._internal({
    required this.startTimeMs,
    required this.autoRenewalEnabled,
    required this.acknowledgementStateCode,
    required this.priceChangeStatusCode,
    required this.cancellationReasonCode,
    required this.userCancellationTimeMs,
    required this.cancellationSurveyResultDto,
    required this.autoResumeTimeMs,
    required this.orderId,
    required this.expirationTimeMs,
    required this.purchaseTypeCode,
  });

  final int startTimeMs;
  final bool autoRenewalEnabled;
  final int acknowledgementStateCode;
  final int? priceChangeStatusCode;
  final int? cancellationReasonCode;
  final int? userCancellationTimeMs;
  final _SubscriptionCancellationSurveyResultDto? cancellationSurveyResultDto;
  final int? autoResumeTimeMs;
  final String orderId;
  final int expirationTimeMs;
  final int? purchaseTypeCode;

  @override
  List<Object?> get props {
    return [
      startTimeMs,
      autoRenewalEnabled,
      acknowledgementStateCode,
      priceChangeStatusCode,
      cancellationReasonCode,
      userCancellationTimeMs,
      cancellationSurveyResultDto,
      autoResumeTimeMs,
      orderId,
      expirationTimeMs,
      purchaseTypeCode,
    ];
  }

  // factory _PurchasedSubscriptionDto.fromJson(Map<String, dynamic> json) {
  //   final userCancellactionTimeData = json['userCancellationTimeMillis'] as String?;
  //   int? userCancellationTimeMs;
  //
  //   if (userCancellactionTimeData != null) {
  //     userCancellationTimeMs = int.parse(userCancellactionTimeData);
  //   }
  //
  //   final priceChangeData = json['priceChange'] as Map<String, dynamic>?;
  //   int? priceChangeStatusCode;
  //
  //   if (priceChangeData != null) {
  //     priceChangeStatusCode = priceChangeData['state'];
  //   }
  //
  //   final autoResumeTimeData = json['autoResumeTimeMillis'] as String?;
  //   int? autoResumeTimeMs;
  //
  //   if (autoResumeTimeData != null) {
  //     autoResumeTimeMs = int.parse(autoResumeTimeData);
  //   }
  //
  //   final cancellationSurveyResultData = json['cancelSurveyResult'] as Map<String, dynamic>?;
  //   _SubscriptionCancellationSurveyResultDto? cancellationSurveyResultDto;
  //
  //   if (cancellationSurveyResultData != null) {
  //     cancellationSurveyResultDto =
  //         _SubscriptionCancellationSurveyResultDto.fromJson(cancellationSurveyResultData);
  //   }
  //
  //   return _PurchasedSubscriptionDto._internal(
  //     startTimeMs: int.parse(json['startTimeMillis']),
  //     autoRenewalEnabled: json['autoRenewing'],
  //     acknowledgementStateCode: json['acknowledgementState'],
  //     priceChangeStatusCode: priceChangeStatusCode,
  //     cancellationReasonCode: json['cancelReason'],
  //     userCancellationTimeMs: userCancellationTimeMs,
  //     cancellationSurveyResultDto: cancellationSurveyResultDto,
  //     autoResumeTimeMs: autoResumeTimeMs,
  //     orderId: json['orderId'],
  //     expirationTimeMs: int.parse(json['expiryTimeMillis']),
  //     purchaseTypeCode: json['purchaseType'],
  //   );
  // }

  factory _PurchasedSubscriptionDto.fromApiSubscriptionPurchase(
      SubscriptionPurchase apiSubscriptionPurchase) {
    final userCancellactionTimeData = apiSubscriptionPurchase.userCancellationTimeMillis;

    int? userCancellationTimeMs;

    if (userCancellactionTimeData != null) {
      userCancellationTimeMs = int.parse(userCancellactionTimeData);
    }

    final priceChangeStatusCode = apiSubscriptionPurchase.priceChange?.state;

    final autoResumeTimeData = apiSubscriptionPurchase.autoResumeTimeMillis;

    int? autoResumeTimeMs;

    if (autoResumeTimeData != null) {
      autoResumeTimeMs = int.parse(autoResumeTimeData);
    }

    _SubscriptionCancellationSurveyResultDto? cancellationSurveyResultDto;

    if (apiSubscriptionPurchase.cancelSurveyResult?.cancelSurveyReason != null) {
      cancellationSurveyResultDto =
          _SubscriptionCancellationSurveyResultDto.fromApiSubscriptionCancelSurveyResult(
              apiSubscriptionPurchase.cancelSurveyResult!);
    }

    return _PurchasedSubscriptionDto._internal(
      startTimeMs: int.parse(apiSubscriptionPurchase.startTimeMillis!),
      autoRenewalEnabled: apiSubscriptionPurchase.autoRenewing!,
      acknowledgementStateCode: apiSubscriptionPurchase.acknowledgementState!,
      priceChangeStatusCode: priceChangeStatusCode,
      cancellationReasonCode: apiSubscriptionPurchase.cancelReason,
      userCancellationTimeMs: userCancellationTimeMs,
      cancellationSurveyResultDto: cancellationSurveyResultDto,
      autoResumeTimeMs: autoResumeTimeMs,
      orderId: apiSubscriptionPurchase.orderId!,
      expirationTimeMs: int.parse(apiSubscriptionPurchase.expiryTimeMillis!),
      purchaseTypeCode: apiSubscriptionPurchase.purchaseType,
    );
  }

  GooglePlayApiPurchasedSubscription toEntity() {
    final startTime = DateTime.fromMillisecondsSinceEpoch(
      startTimeMs,
      isUtc: true,
    );

    final expirationTime = DateTime.fromMillisecondsSinceEpoch(
      expirationTimeMs,
      isUtc: true,
    );

    final GooglePlayApiPurchasedSubscriptionOfferType offerType;

    final test = purchaseTypeCode == 0;

    if (test) {
      offerType = GooglePlayApiPurchasedSubscriptionOfferType.regular;
    } else {
      if (expirationTime.difference(startTime) <= const Duration(days: 7)) {
        offerType = GooglePlayApiPurchasedSubscriptionOfferType.trialPeriod;
      } else if (purchaseTypeCode == 1) {
        offerType = GooglePlayApiPurchasedSubscriptionOfferType.promotional;
      } else {
        offerType = GooglePlayApiPurchasedSubscriptionOfferType.regular;
      }
    }

    late final GooglePlayApiPurchasedSubscriptionPriceChangeStatus priceChangeStatus;

    if (priceChangeStatusCode == null) {
      priceChangeStatus = GooglePlayApiPurchasedSubscriptionPriceChangeStatus.absent;
    } else if (priceChangeStatusCode == 0) {
      priceChangeStatus = GooglePlayApiPurchasedSubscriptionPriceChangeStatus.pending;
    } else if (priceChangeStatusCode == 1) {
      priceChangeStatus = GooglePlayApiPurchasedSubscriptionPriceChangeStatus.accepted;
    }

    GooglePlayApiPurchasedSubscriptionCancellationInfo? cancellationInfo;

    if (cancellationReasonCode == 0) {
      final time = DateTime.fromMillisecondsSinceEpoch(
        userCancellationTimeMs!,
        isUtc: true,
      );

      late final GooglePlayApiPurchasedSubscriptionCancellationSurveyResult surveyResult;

      if (cancellationSurveyResultDto == null) {
        surveyResult = const GooglePlayApiPurchasedSubscriptionCancellationSurveyResult(
          reason: GooglePlayApiPurchasedSubscriptionCancellationSurveyReason.unspecified,
          customAnswer: null,
        );
      } else {
        final reasonCode = cancellationSurveyResultDto!.reasonCode;

        if (reasonCode == 0) {
          surveyResult = GooglePlayApiPurchasedSubscriptionCancellationSurveyResult(
            reason: GooglePlayApiPurchasedSubscriptionCancellationSurveyReason.other,
            customAnswer: cancellationSurveyResultDto!.customAnswer,
          );
        } else if (reasonCode == 1) {
          surveyResult = const GooglePlayApiPurchasedSubscriptionCancellationSurveyResult(
            reason: GooglePlayApiPurchasedSubscriptionCancellationSurveyReason.insufficientUse,
            customAnswer: null,
          );
        } else if (reasonCode == 2) {
          surveyResult = const GooglePlayApiPurchasedSubscriptionCancellationSurveyResult(
            reason: GooglePlayApiPurchasedSubscriptionCancellationSurveyReason.technicalIssues,
            customAnswer: null,
          );
        } else if (reasonCode == 3) {
          surveyResult = const GooglePlayApiPurchasedSubscriptionCancellationSurveyResult(
            reason: GooglePlayApiPurchasedSubscriptionCancellationSurveyReason.cost,
            customAnswer: null,
          );
        } else if (reasonCode == 4) {
          surveyResult = const GooglePlayApiPurchasedSubscriptionCancellationSurveyResult(
            reason: GooglePlayApiPurchasedSubscriptionCancellationSurveyReason.notBestApp,
            customAnswer: null,
          );
        }
      }

      cancellationInfo = GooglePlayApiPurchasedSubscriptionUserCancellationInfo(
        time: time,
        surveyResult: surveyResult,
      );
    } else if (cancellationReasonCode == 1) {
      cancellationInfo = const GooglePlayApiPurchasedSubscriptionSystemCancellationInfo();
    } else if (cancellationReasonCode == 2) {
      cancellationInfo = const GooglePlayApiPurchasedSubscriptionNewSubscriptionCancellationInfo();
    } else if (cancellationReasonCode == 3) {
      cancellationInfo = const GooglePlayApiPurchasedSubscriptionDeveloperCancellationInfo();
    }

    GooglePlayApiPurchasedSubscriptionPauseInfo? pauseInfo;

    if (autoResumeTimeMs != null) {
      final autoResumeTime = DateTime.fromMillisecondsSinceEpoch(
        autoResumeTimeMs!,
        isUtc: true,
      );

      pauseInfo = GooglePlayApiPurchasedSubscriptionPauseInfo(
        autoResumeTime: autoResumeTime,
      );
    }

    return GooglePlayApiPurchasedSubscription(
      test: test,
      startTime: startTime,
      autoRenewalEnabled: autoRenewalEnabled,
      acknowledged: acknowledgementStateCode == 1,
      priceChangeStatus: priceChangeStatus,
      cancellationInfo: cancellationInfo,
      pauseInfo: pauseInfo,
      orderId: orderId,
      expirationTime: expirationTime,
      offerType: offerType,
    );
  }
}
